# Define the main executable
add_executable(xsan_node
    xsan_node.c
)

# Add XSAN include directories
target_include_directories(xsan_node PUBLIC
    ${CMAKE_SOURCE_DIR}/include     # For xsan.h, xsan_types.h
    ${CMAKE_SOURCE_DIR}/src/include # For all module headers (xsan_spdk_manager.h, xsan_bdev.h, etc.)
)

# Link against our XSAN static libraries
target_link_libraries(xsan_node PRIVATE
    xsan_core       # For SPDK management
    xsan_bdev       # For BDEV operations
    xsan_network    # For socket, event_loop, protocol, node_comm
    xsan_storage    # For disk_manager, volume_manager
    xsan_vhost      # For vhost integration
    xsan_cluster    # For cluster operations (xsan_get_local_node_info, etc.)
    xsan_common     # For error, version, common data structures
    xsan_utils      # For config, log, memory, string_utils
    Threads::Threads # From find_package(Threads REQUIRED) in root
)

# Conditionally link SPDK libraries and apply SPDK specific settings
if(XSAN_ENABLE_SPDK)
    message(STATUS "Configuring xsan_node target with SPDK support.")
    # XSAN_SPDK_INCLUDE_DIRS are already added globally in the root CMakeLists.txt via include_directories(SYSTEM ...)
    # XSAN_SPDK_CFLAGS are already added globally in the root CMakeLists.txt via add_compile_options(...)

    # Add SPDK library directories for linking (e.g., -L/path/to/spdk/lib)
    # These are from XSAN_SPDK_LIBRARY_DIRS set in the root CMakeLists.txt
    target_link_directories(xsan_node PRIVATE ${XSAN_SPDK_LIBRARY_DIRS})

    # 先链接 ISA-L（必须在 SPDK 之前）
    if(XSAN_ISAL_LIBRARIES)
        target_link_libraries(xsan_node PRIVATE ${XSAN_ISAL_LIBRARIES})
    endif()
    
    # whole-archive for static SPDK (自动判断)
    if(EXISTS "${XSAN_SPDK_LIBRARY_DIRS}/libspdk_util.a")
        target_link_options(xsan_node PRIVATE -Wl,--whole-archive)
        target_link_libraries(xsan_node PRIVATE ${XSAN_SPDK_LIBRARIES})
        target_link_options(xsan_node PRIVATE -Wl,--no-whole-archive)
    else()
        target_link_libraries(xsan_node PRIVATE ${XSAN_SPDK_LIBRARIES})
    endif()
    
    target_link_libraries(xsan_node PRIVATE ${XSAN_DPDK_LIBRARIES})
    target_link_libraries(xsan_node PRIVATE ${XSAN_OPENSSL_LIBRARIES})
    if(XSAN_NUMA_LIBRARIES)
        target_link_libraries(xsan_node PRIVATE ${XSAN_NUMA_LIBRARIES})
    endif()
    target_link_libraries(xsan_node PRIVATE dl m rt pthread)


    # If SPDK requires specific C standards or features not enabled globally:
    # target_compile_features(xsan_node PRIVATE c_std_11) # Example if needed

    # If SPDK static libraries require --whole-archive, it's complex with pkg-config.
    # SPDK's pkg-config files for static linking *should* ideally include these flags
    # in LIBS_PRIVATE or similar. If not, manual adjustment is needed.
    # Example:
    # target_link_libraries(xsan_node PRIVATE -Wl,--whole-archive spdk_some_static_lib -Wl,--no-whole-archive)
    # This is highly dependent on the SPDK build and installation.
    # For now, assume pkg-config handles it.

    message(STATUS "xsan_node target configured to link with SPDK.")
else()
    message(STATUS "xsan_node target configured WITHOUT SPDK support.")
endif()

# Define the CLI executable
add_executable(xsan_cli
    xsan_cli.c
)

# Add XSAN include directories for CLI
target_include_directories(xsan_cli PUBLIC
    ${CMAKE_SOURCE_DIR}/include     # For xsan.h, xsan_types.h
    ${CMAKE_SOURCE_DIR}/src/include # For all module headers
)

# Link CLI against basic XSAN libraries
target_link_libraries(xsan_cli PRIVATE
    xsan_core
    xsan_bdev
    xsan_network
    xsan_storage
    xsan_vhost
    xsan_cluster    # For cluster operations
    xsan_common     # For error, version, common data structures
    xsan_utils      # For config, log, memory, string_utils
    Threads::Threads # From find_package(Threads REQUIRED) in root
)

# Conditionally link SPDK libraries and apply SPDK specific settings for CLI
if(XSAN_ENABLE_SPDK)
    # 先链接 ISA-L（必须在 SPDK 之前）
    if(XSAN_ISAL_LIBRARIES)
        target_link_libraries(xsan_cli PRIVATE ${XSAN_ISAL_LIBRARIES})
    endif()
    
    target_link_libraries(xsan_cli PRIVATE ${XSAN_SPDK_LIBRARIES})
    target_link_libraries(xsan_cli PRIVATE ${XSAN_DPDK_LIBRARIES})
    target_link_libraries(xsan_cli PRIVATE ${XSAN_OPENSSL_LIBRARIES})
    if(XSAN_NUMA_LIBRARIES)
        target_link_libraries(xsan_cli PRIVATE ${XSAN_NUMA_LIBRARIES})
    endif()
    target_link_libraries(xsan_cli PRIVATE dl m rt)
endif()

# Installation (optional example)
# install(TARGETS xsan_node xsan_cli DESTINATION bin)
# install(FILES ${CMAKE_SOURCE_DIR}/config/xsan_default.conf DESTINATION etc/xsan)
